using System;
using System.Data;
using System.Data.SqlClient;
using com.Norland.SPMS.Common;
using com.Norland.SPMS.Common.Data;
using System.Text;

namespace com.Norland.SPMS.DataAccess
{
	/// <summary>
	/// 
	/// </summary>
	public class Autonum : IDisposable
	{
		private SqlCommand loadCommand=null;
		private SqlCommand insertCommand=null;
		private SqlCommand updateCommand=null;
		private SqlConnection conc;
		
		public Autonum()
		{
			conc = new SqlConnection(SPMSConfiguration.DbConnectionString);
		}

		/// <summary>
		///     Dispose of this object's resources.
		/// </summary>
		public void Dispose() {
			Dispose(true);
			GC.SuppressFinalize(true); // as a service to those who might inherit from us
		}

		/// <summary>
		///		Free the instance variables of this object.
		/// </summary>
		protected virtual void Dispose(bool disposing) {
			if (! disposing)
				return; // we're being collected, so let the GC take care of this object

		
			if(loadCommand != null) {
				loadCommand.Dispose();
			}   
 
			if(insertCommand != null) {
				insertCommand.Dispose();
			} 

			if(updateCommand != null) {
				updateCommand.Dispose();
			} 
		}

		/// <summary>
		/// 
		/// </summary>
		public bool FetchAuto(string Key, string Prefix,ref com.Norland.SPMS.Common.Data.AutoData.Params myparams) {
			bool bi=false;
			string sqlstr = @"select * from " + Common.Data.AutoData.AUTONUM_TABLE
				+ " where " + Common.Data.AutoData.ANFKEY + "=@Key"
				+ " and " + Common.Data.AutoData.ANFPREFIX + "=@Prefix";

			loadCommand = new SqlCommand(sqlstr,conc);
			loadCommand.Parameters.Add("@Key",System.Data.SqlDbType.VarChar).Value = Key;
			loadCommand.Parameters.Add("@Prefix",System.Data.SqlDbType.VarChar).Value = Prefix;

		//	com.darwin.Common.Data.AutoData.Params myparams;
			conc.Open();
			
			try{
				
				SqlDataReader reader = loadCommand.ExecuteReader(CommandBehavior.CloseConnection);
				if (reader.HasRows) {
					reader.Read();
					myparams.prefix = reader[com.Norland.SPMS.Common.Data.AutoData.ANFPREFIX].ToString();
					myparams.max = (int)reader[com.Norland.SPMS.Common.Data.AutoData.ANFMAX];
					myparams.min = (int)reader[com.Norland.SPMS.Common.Data.AutoData.ANFMIN];
					myparams.interval = (int)reader[com.Norland.SPMS.Common.Data.AutoData.ANFINTERVAL];
					myparams.digitswidth = (int)reader[com.Norland.SPMS.Common.Data.AutoData.ANFDIGITSWIDTH];
					myparams.digits = (int)reader[com.Norland.SPMS.Common.Data.AutoData.ANFDIGITS];
					bi = true;
				}
				reader.Close();
			}finally{
				conc.Close();
			}
			
			return bi;
		}

		public bool UpdateAuto(com.Norland.SPMS.Common.Data.AutoData.Params OldParams, int NewDigit) {
			bool ui = false;
			string sqlstr = @"Update " + com.Norland.SPMS.Common.Data.AutoData.AUTONUM_TABLE + " set " + com.Norland.SPMS.Common.Data.AutoData.ANFDIGITS
				+ "=@NewD where " + com.Norland.SPMS.Common.Data.AutoData.ANFKEY + "=@Key and " + com.Norland.SPMS.Common.Data.AutoData.ANFPREFIX + "=@Prefix and "
				+ com.Norland.SPMS.Common.Data.AutoData.ANFDIGITS +"=@OldD";

			updateCommand = new SqlCommand(sqlstr,conc);
			updateCommand.Parameters.Add("@NewD",System.Data.SqlDbType.Int).Value = NewDigit;
			updateCommand.Parameters.Add("@Key",System.Data.SqlDbType.VarChar).Value = OldParams.key;
			updateCommand.Parameters.Add("@Prefix",System.Data.SqlDbType.VarChar).Value = OldParams.prefix;
			updateCommand.Parameters.Add("@OldD",System.Data.SqlDbType.Int).Value = OldParams.digits;

			conc.Open();
			try{
				ui = updateCommand.ExecuteNonQuery() > 0;
			}finally{
				conc.Close();
			}
			return ui;
		}

		public void AppendAuto(com.Norland.SPMS.Common.Data.AutoData.Params UParams) {
			string sqlstr = @"insert into " + com.Norland.SPMS.Common.Data.AutoData.AUTONUM_TABLE + " (" + com.Norland.SPMS.Common.Data.AutoData.ANFKEY
				+ "," + com.Norland.SPMS.Common.Data.AutoData.ANFPREFIX + "," + Common.Data.AutoData.ANFMIN + "," + com.Norland.SPMS.Common.Data.AutoData.ANFMAX
				+ "," + com.Norland.SPMS.Common.Data.AutoData.ANFDIGITS + "," + Common.Data.AutoData.ANFDIGITSWIDTH + ","
				+ com.Norland.SPMS.Common.Data.AutoData.ANFINTERVAL + ") values(@Key,@Prefix,@Min,@Max,@Digits,@DigW,@Interval)";

			insertCommand = new SqlCommand(sqlstr,conc);
			insertCommand.Parameters.Add("@Key",System.Data.SqlDbType.VarChar).Value = UParams.key;
			insertCommand.Parameters.Add("@Prefix",System.Data.SqlDbType.VarChar).Value = UParams.prefix;
			insertCommand.Parameters.Add("@Min",System.Data.SqlDbType.Int).Value = UParams.min;
			insertCommand.Parameters.Add("@Max",System.Data.SqlDbType.Int).Value = UParams.max;
			insertCommand.Parameters.Add("@Digits",System.Data.SqlDbType.Int).Value = UParams.digits;
			insertCommand.Parameters.Add("@DigW",System.Data.SqlDbType.Int).Value = UParams.digitswidth;
			insertCommand.Parameters.Add("@Interval",System.Data.SqlDbType.Int).Value = UParams.interval;

			conc.Open();
			try{
				insertCommand.ExecuteNonQuery();
			}finally{
				conc.Close();
			}
		}
	}
}
